<?php

namespace app\admin\controller\upload;

use app\admin\controller\AdminController;
use tmcore\services\ConfigService;
use tmcore\services\UploadService;
use Exception;
use think\response\Json;

/**
 * 上传文件
 */
class Upload extends AdminController
{
    /**
     * 上传图片
     * @return Json
     */
    public function image()
    {
        try {
            $params = $this->paramData([
                ['file', 'file'],
                ['name', ''],
                ['multiple', false],
                ['size', 0],
                ['ext', ''],
                ['cid', 0],
                ['multiple', false],
                ['save_dir', '']
            ]);
            $params['source_id'] = $this->adminId;
            $result = (new UploadService)->upload('image', $params);
            return $this->success($result, '上传成功');
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 上传视频
     * @return Json
     */
    public function video()
    {
        try {
            $params = $this->paramData([
                ['file', 'file'],
                ['name', ''],
                ['size', 0],
                ['ext', ''],
                ['cid', 0],
                ['multiple', false],
                ['save_dir', '']
            ]);
            $params['source_id'] = $this->adminId;
            $result = (new UploadService)->upload('video', $params);
            return $this->success($result, '上传成功');
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 上传文件
     * @return Json
     */
    public function file()
    {
        try {
            $params = $this->paramData([
                ['file', 'file'],
                ['name', ''],
                ['size', 0],
                ['ext', ''],
                ['cid', 0],
                ['multiple', false],
                ['save_dir', '']
            ]);
            $params['source_id'] = $this->adminId;
            $result = (new UploadService)->upload('file', $params);
            return $this->success($result, '上传成功');
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

}